﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CanDoo;
using CanDoo.Core;
using CanDoo.Core.Data;
using CanDoo.Data;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Oil.Core.Models;

namespace CanDoo.Oil.Core.Services
{
    /// <summary>
    /// 业务实现——临时产品序列号
    /// </summary>
    public class SerialNumTempService : CanDoo.Data.Entity.Services.ServiceBase<SerialNumTemp>, ISerialNumTempContract
    {
        public IOilSalesSerialNumContract OilSalesSerialNumContract { get; set; }
        public IOilSerialNumContract OilSerialNumContract { get; set; }
        public ISerialNumContract SerialNumContract { get; set; }
        public SerialNumTempService()
        {
        
        }

        /// <summary>
        /// 采购入库审核时临时库中的序列号移到正式库
        /// </summary>
        /// <param name="EntityId">单据Id</param>
        /// <returns></returns>
        public OperationResult PassInStore(long EntityId)
        {
            var result = new OperationResult(OperationResultType.NoChanged);
            var entityList = Entities.Where(p => p.InStoreId == EntityId).ToList();
            if (entityList != null && entityList.Count() > 0)
            {
                var InTypeName = entityList[0].InStoreType.Name;

                var serialNumList = new List<SerialNum>();
                foreach (var m in entityList.Where(p => p.SerialNumType == 0))
                {
                    var mEntity = new SerialNum();
                    mEntity.Id = IdMaker.NewId();
                    mEntity.Name = "[" + InTypeName + "]" + m.SerialNumber;
                    mEntity.WarehouseId = m.WarehouseId;
                    mEntity.InStoreTypeId = m.InStoreTypeId;
                    mEntity.InStoreId = m.InStoreId;
                    mEntity.ProductId = m.ProductId;
                    mEntity.SerialNumber = m.SerialNumber;
                    mEntity.Status = 1;
                    mEntity.Batch = m.Batch;
                    serialNumList.Add(mEntity);
                }

                var oilSerialNumList = new List<OilSerialNum>();
                foreach (var m in entityList.Where(p => p.SerialNumType == 1))
                {
                    var mEntity = new OilSerialNum();
                    mEntity.Id = IdMaker.NewId();
                    mEntity.Name = "[" + InTypeName + "]" + m.SerialNumber;
                    mEntity.WarehouseId = m.WarehouseId;
                    mEntity.InStoreTypeId = m.InStoreTypeId;
                    mEntity.InStoreId = m.InStoreId;
                    mEntity.LastOil = m.Product.Weight*1000;
                    mEntity.ProductId = m.ProductId;
                    mEntity.SerialNumber = m.SerialNumber;
                    mEntity.Status = OilSerialStatus.PassIn;
                    mEntity.Batch = m.Batch;
                    oilSerialNumList.Add(mEntity);
                }

                //var oilSalesSerialNumList = new List<OilSalesSerialNum>();
                //foreach (var m in entityList.Where(p => p.SerialNumType == 2))
                //{
                //    var mEntity = new OilSalesSerialNum();
                //    mEntity.Id = IdMaker.NewId();
                //    mEntity.Name = "[" + InTypeName + "]" + m.SerialNumber;
                //    mEntity.WarehouseId = m.WarehouseId;
                //    mEntity.InStoreTypeId = m.InStoreTypeId;
                //    mEntity.InStoreId = m.InStoreId;
                //    mEntity.ProductId = m.ProductId;
                //    mEntity.SerialNumber = m.SerialNumber;
                //    mEntity.Status = 1;
                //    mEntity.Batch = m.Batch;
                //    oilSalesSerialNumList.Add(mEntity);
                //}

                //OilSalesSerialNumContract.AddEntity(oilSalesSerialNumList);
                OilSerialNumContract.AddEntity(oilSerialNumList);
                SerialNumContract.AddEntity(serialNumList);
                DeleteEntity(p => p.InStoreId == EntityId);
            }
            return result;
        }


        /// <summary>
        /// 取消采购入库审核时正式库中的序列号移到临时库
        /// </summary>
        /// <param name="EntityId">单据Id</param>
        /// <returns></returns>
        public OperationResult UnPassInStore(long EntityId)
        {
            var result = new OperationResult(OperationResultType.NoChanged);
            var serialNumList = SerialNumContract.Entities.Where(p => p.InStoreId == EntityId).ToList();
            var oilSerialNumList = OilSerialNumContract.Entities.Where(p => p.InStoreId == EntityId).ToList();
            var serialNumTmepList = new List<SerialNumTemp>();

            if (serialNumList != null && serialNumList.Count() > 0)
            {
                foreach (var m in serialNumList)
                {
                    if (m.Status > 1)
                        throw new Exception("序列号[" + m.SerialNumber + "]已使用");

                    var mEntity = new SerialNumTemp();
                    mEntity.Id = IdMaker.NewId();
                    mEntity.Name = m.Name;
                    mEntity.WarehouseId = m.WarehouseId;
                    mEntity.InStoreTypeId = m.InStoreTypeId;
                    mEntity.InStoreId = m.InStoreId;
                    mEntity.ProductId = m.ProductId;
                    mEntity.SerialNumber = m.SerialNumber;
                    mEntity.Status = 0;
                    mEntity.Batch = m.Batch;
                    mEntity.SerialNumType = 0;
                    serialNumTmepList.Add(mEntity);
                }
            }

            if (oilSerialNumList != null && oilSerialNumList.Count() > 0)
            {
                foreach (var m in oilSerialNumList)
                {
                    if (m.Status > OilSerialStatus.PassIn)
                        throw new Exception("序列号[" + m.SerialNumber + "]已使用");

                    var mEntity = new SerialNumTemp();
                    mEntity.Id = IdMaker.NewId();
                    mEntity.Name = m.Name;
                    mEntity.WarehouseId = m.WarehouseId;
                    mEntity.InStoreTypeId = m.InStoreTypeId;
                    mEntity.InStoreId = m.InStoreId;
                    mEntity.ProductId = m.ProductId;
                    mEntity.SerialNumber = m.SerialNumber;
                    mEntity.Status = 0;
                    mEntity.Batch = m.Batch;
                    mEntity.SerialNumType = 1;
                    serialNumTmepList.Add(mEntity);
                }
            }

            OilSerialNumContract.DeleteEntity(p => p.InStoreId == EntityId);
            SerialNumContract.DeleteEntity(p => p.InStoreId == EntityId);
            AddEntity(serialNumTmepList);
            return result;
        }

        /// <summary>
        /// 采购退货单审核时从正式库删除
        /// </summary>
        /// <param name="EntityId"></param>
        /// <returns></returns>
        public OperationResult PassInBuyBack(long EntityId)
        {
            var result = OperationResult.Success;
            var serialNumTempList = new List<SerialNumTemp>();
            var serialNumList = SerialNumContract.Entities.Where(p => p.InBuyBackId == EntityId).ToList();
            var oilSerialNumList = OilSerialNumContract.Entities.Where(p => p.InBuyBackId == EntityId).ToList();
            foreach (var m in serialNumList)
            {
                var mEntity = new SerialNumTemp();
                mEntity.Id = IdMaker.NewId();
                mEntity.Name = m.Name;
                mEntity.WarehouseId = m.WarehouseId;
                mEntity.InStoreTypeId = m.InStoreTypeId;
                mEntity.InStoreId = m.InStoreId;
                mEntity.InBuyBackId = EntityId;
                mEntity.ProductId = m.ProductId;
                mEntity.SerialNumber = m.SerialNumber;
                mEntity.Status = 3;
                mEntity.Batch = m.Batch;
                mEntity.SerialNumType = 0;
                serialNumTempList.Add(mEntity);
                SerialNumContract.Delete(m.Id);
            }

            foreach (var m in oilSerialNumList)
            {
                var mEntity = new SerialNumTemp();
                mEntity.Id = IdMaker.NewId();
                mEntity.Name = m.Name;
                mEntity.WarehouseId = m.WarehouseId;
                mEntity.InStoreTypeId = m.InStoreTypeId;
                mEntity.InStoreId = m.InStoreId;
                mEntity.InBuyBackId = EntityId;
                mEntity.ProductId = m.ProductId;
                mEntity.SerialNumber = m.SerialNumber;
                mEntity.Status = 3;
                mEntity.Batch = m.Batch;
                mEntity.SerialNumType = 1;
                serialNumTempList.Add(mEntity);
                OilSerialNumContract.Delete(m.Id);
            }
            AddEntity(serialNumTempList);

            //var serialNumTmepList = Entities.Where(p => p.InStoreId == EntityId).ToList();
            //foreach (var m in serialNumTmepList)
            //{
            //    if (m.SerialNumType == 0)
            //    {
            //        var n = SerialNumContract.Entities.FirstOrDefault(p => p.SerialNumber == m.SerialNumber);
            //        if(n==null)
            //            throw new Exception("序列号[" + n.SerialNumber + "]不存在");

            //        if (n.Status > 1)
            //            throw new Exception("序列号[" + n.SerialNumber + "]已使用");
            //        else
            //        {
            //            m.OldInStoreId = n.InStoreId;
            //            m.OldInStoreTypeId = n.InStoreTypeId;
            //            SerialNumContract.DeleteEntity(n.Id);
            //        }
            //    }
            //    else if (m.SerialNumType == 1)
            //    {
            //        var n = OilSerialNumContract.Entities.FirstOrDefault(p => p.SerialNumber == m.SerialNumber);
            //        if (n == null)
            //            throw new Exception("序列号[" + n.SerialNumber + "]不存在");

            //        if (n.Status > OilSerialStatus.PassIn)
            //            throw new Exception("序列号[" + n.SerialNumber + "]已使用");
            //        else
            //        {
            //            m.OldInStoreId = n.InStoreId;
            //            m.OldInStoreTypeId = n.InStoreTypeId;
            //            OilSerialNumContract.DeleteEntity(n.Id);
            //        }
            //    }
            //    else if (m.SerialNumType == 2)
            //    {
            //        //售油机序列号是后添加的 所以不需要处理
            //    }
            //    else
            //        throw new Exception("参数错误，请联系开发人员");
            //}

            return result;
        }


        /// <summary>
        /// 采购退货单取消审核时恢复到正式库
        /// </summary>
        /// <param name="EntityId"></param>
        /// <returns></returns>
        public OperationResult UnPassInBuyBack(long EntityId)
        {
            var result = OperationResult.Success;
            var entityList = Entities.Where(p => p.InBuyBackId == EntityId).ToList();

            if (entityList != null && entityList.Count() > 0)
            {
                var serialNumList = new List<SerialNum>();
                foreach (var m in entityList.Where(p => p.SerialNumType == 0))
                {
                    var mEntity = new SerialNum();
                    mEntity.Id = IdMaker.NewId();
                    mEntity.Name = m.Name;
                    mEntity.WarehouseId = m.WarehouseId;
                    mEntity.InStoreTypeId = m.InStoreTypeId;
                    mEntity.InStoreId = m.InStoreId;
                    mEntity.InBuyBackId = m.InBuyBackId;
                    mEntity.ProductId = m.ProductId;
                    mEntity.SerialNumber = m.SerialNumber;
                    mEntity.Status = 2;
                    mEntity.Batch = m.Batch;
                    serialNumList.Add(mEntity);
                    Delete(m.Id);
                }

                var oilSerialNumList = new List<OilSerialNum>();
                foreach (var m in entityList.Where(p => p.SerialNumType == 1))
                {
                    var mEntity = new OilSerialNum();
                    mEntity.Id = IdMaker.NewId();
                    mEntity.Name = "[采购退货撤销]" + m.SerialNumber;
                    mEntity.WarehouseId = m.WarehouseId;
                    mEntity.InStoreTypeId = m.InStoreTypeId;
                    mEntity.InStoreId = m.InStoreId;
                    mEntity.InBuyBackId = m.InBuyBackId;
                    mEntity.ProductId = m.ProductId;
                    mEntity.SerialNumber = m.SerialNumber;
                    mEntity.Status = OilSerialStatus.WaitBuyBack;
                    mEntity.Batch = m.Batch;
                    oilSerialNumList.Add(mEntity);
                    Delete(m.Id);
                }

                //var oilSalesSerialNumList = new List<OilSalesSerialNum>();
                //foreach (var m in entityList.Where(p => p.SerialNumType == 2))
                //{
                //    var mEntity = new OilSalesSerialNum();
                //    mEntity.Id = IdMaker.NewId();
                //    mEntity.Name = "[" + InTypeName + "]" + m.SerialNumber;
                //    mEntity.WarehouseId = m.WarehouseId;
                //    mEntity.InStoreTypeId = m.InStoreTypeId;
                //    mEntity.InStoreId = m.InStoreId;
                //    mEntity.ProductId = m.ProductId;
                //    mEntity.SerialNumber = m.SerialNumber;
                //    mEntity.Status = 1;
                //    mEntity.Batch = m.Batch;
                //    oilSalesSerialNumList.Add(mEntity);
                //}

                //OilSalesSerialNumContract.AddEntity(oilSalesSerialNumList);
                OilSerialNumContract.AddEntity(oilSerialNumList);
                SerialNumContract.AddEntity(serialNumList);
                //UpdateEntity(entityList);
            }

            return result;
        }

        /// <summary>
        /// 销售出库单取消审核时将状态改为等待出库审核状态
        /// </summary>
        /// <param name="EntityId"></param>
        /// <returns></returns>
        public OperationResult UnPassOutStore(long EntityId)
        {
            var result = OperationResult.Success;
            var serialNumList = SerialNumContract.Entities.Where(p => p.OutStoreId == EntityId);
            foreach (var m in serialNumList)
            {
                if (m.Status != 5)
                    throw new Exception("序列号[" + m.SerialNumber + "]已使用");
                else
                    m.Status = 4;
                SerialNumContract.UpdateEntity(m);
            }

            var oilSerialNumList = OilSerialNumContract.Entities.Where(p => p.OutStoreId == EntityId);
            foreach (var m in oilSerialNumList)
            {
                if (m.Status != OilSerialStatus.PassOut)
                    throw new Exception("序列号[" + m.SerialNumber + "]已使用");
                else
                    m.Status = OilSerialStatus.WaitOut;
                OilSerialNumContract.UpdateEntity(m);
            }

            return result;
        }


        /// <summary>
        /// 客户退货单取消审核时将状态改为等待退货审核状态
        /// </summary>
        /// <param name="EntityId"></param>
        /// <returns></returns>
        public OperationResult UnPassOutSellBack(long EntityId)
        {
            var result = OperationResult.Success;
            var serialNumList = SerialNumContract.Entities.Where(p => p.OutSellBackId == EntityId);
            foreach (var m in serialNumList)
            {
                if (m.Status != 7)
                    throw new Exception("序列号[" + m.SerialNumber + "]已使用");
                else
                    m.Status = 6;
                SerialNumContract.UpdateEntity(m);
            }

            var oilSerialNumList = OilSerialNumContract.Entities.Where(p => p.OutSellBackId == EntityId);
            foreach (var m in oilSerialNumList)
            {
                if (m.Status != OilSerialStatus.Back)
                    throw new Exception("序列号[" + m.SerialNumber + "]已使用");
                else
                    m.Status = OilSerialStatus.WaitBack;
                OilSerialNumContract.UpdateEntity(m);
            }

            return result;
        }

    }
}
